package org.veridiandynamics.server.domain;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
/**
 * Schedule class containing appointments; linked to a particular doctor.
 *
 */
public class Schedule implements PersistObject, Serializable
{
	private Set<Appointment> mySchedule;
	private long id;

	private static final long serialVersionUID = 90L;
	
	/**
	 * Sets schedule.
	 * 
	 * @param mySchedule
	 */
	public void setMySchedule(Set<Appointment> mySchedule) {
		this.mySchedule = mySchedule;
	}

	/**
	 * Default constructor.
	 */
	public Schedule() {
		
	}
	
	/**
	 * Checks for conflicts with previously existing appointments in schedule before new appointment can be made.
	 * @param newAppointment Desired appointment to be made.
	 * @return Whether or not a conflict exists.
	 */
	public boolean checkConflict(Appointment newAppointment){
		Boolean conflict = false;
		Iterator<Appointment> iterator = mySchedule.iterator();
		while(iterator.hasNext()) {
			Appointment checkAppointment = iterator.next();
			Date oldStartDate = checkAppointment.getStartDate();
			Date oldEndDate = checkAppointment.getEndDate();
			Date newStartDate = newAppointment.getStartDate();
			Date newEndDate = newAppointment.getEndDate();
			if(newStartDate.before(oldStartDate) && newEndDate.after(oldStartDate)
					&& newEndDate.before(oldEndDate)) {
				conflict = true;
			}
			if(newStartDate.after(oldStartDate) && newStartDate.before(oldEndDate)
					&& newEndDate.after(oldEndDate)) {
				conflict = true;
			}
			if(newStartDate.after(oldStartDate) && newStartDate.before(oldEndDate)
					&& newEndDate.after(oldStartDate) && newEndDate.before(oldEndDate)) {
				conflict = true;
			}
			if(newStartDate.before(oldStartDate) && newEndDate.after(oldEndDate)) {
				conflict = true;
			}
			if(oldStartDate.equals(newStartDate) || oldEndDate.equals(newEndDate)) {
				conflict = true;
			}
			
		}
		return conflict;
	}
	

	
	/**
	 * Returns set of Appointment objects in this instance of schedule.
	 * 
	 * @return Set of Appointment objects
	 */
	public Set<Appointment> getMySchedule() {
		return mySchedule;
	}
	/**
	 * Sets Set of Appointment objects in this schedule
	 * @param schedule Set of Appointment objects
	 */
	public void setSchedule(Set<Appointment> schedule) {
		this.mySchedule = schedule;
	}
	/**
	 * Adds a single appointment to schedule.
	 * @param appointment Appointment object to be added
	 */
	public void addAppointment(Appointment appointment){
		this.mySchedule.add(appointment);
	}
	
	/**
	 * Returns ID for an instance of Schedule
	 * @return variable of type long containing ID
	 */
	public long getId() {
		return id;
	}

	/**
	 * Sets ID 
	 * @param id
	 */
	public void setId(long id) {
		this.id = id;
	}
	
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		String retVal = "";
		for (Appointment e: mySchedule)
		{
			retVal += e.toString() + "\n" + "\n";
		}
		
		return retVal;
	}
	

	
	/**
	 * Writes object.
	 * 
	 * @param outputStream
	 * @throws IOException
	 */
	private void writeObject(ObjectOutputStream outputStream) throws IOException {
		outputStream.defaultWriteObject();
	}
	
	/**
	 * Attempts to read object.
	 * 
	 * @param inputStream
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException {
		inputStream.defaultReadObject();
	}
}

